home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8259 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: const char??
  5. Date: Sat, 02 Mar 96 14:08:50 GMT
  6. Organization: none
  7. Message-ID: <825775730snz@genesis.demon.co.uk>
  8. References: <31287436.1235873@news.inforamp.net> <TANMOY.96Feb20095538@qcd.lanl.gov> <4gqflgINN1kq@keats.ugrad.cs.ubc.ca> <825454039snz@genesis.demon.co.uk> <4h27ruINN4lo@anvil.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4h27ruINN4lo@anvil.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >Ah, I see. That makes sense, since strchr() has to manufacture a pointer
  18. >somewhere along the line, through which access to the string s could take
  19. >place. What if it is done immediately in the return statement of strchr()? As
  20. >in:
  21. >
  22. >        return (char *) x;      /* x is (const char *)  */
  23. >
  24. >That's one way to write the function which ensures that it never has in its
  25. >possession a pointer through which the string could be legally modified.
  26.  
  27. It doesn't really matter where it does the conversion since strchr() doesn't
  28. modify the string. The problem remains however that this returns a pointer
  29. with the const qualifier removed so that the calling function can write
  30. through it directly. This is an example where the standard library defeats
  31. the type system and undermines the protection that const is designed to
  32. gives you.
  33.  
  34. As I mentioned before having strchr() return an offset rather than a pointer
  35. would have eliminated this problem. However it would have made dealing with
  36. match failures messier.
  37.  
  38. -- 
  39. -----------------------------------------
  40. Lawrence Kirby | fred@genesis.demon.co.uk
  41. Wilts, England | 70734.126@compuserve.com
  42. -----------------------------------------
  43.